package com.geovis.emergency.spd.biz.pc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.common.auth.util.AuthUtil;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.pc.mapper.PcDrillPlanDetailMapper;
import com.geovis.emergency.spd.biz.pc.mapper.PcDrillPlanTaskMapper;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillDisasterEventService;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillPlanDetailService;
import com.geovis.emergency.spd.cache.DictCache;
import com.geovis.emergency.spd.entity.mobile.pojo.vo.DrillPlanTaskMobileVO;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillDisasterEvent;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPlanDetail;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcDisasterConstant;
import com.geovis.emergency.spd.entity.pc.pojo.enums.CheckStatusEnum;
import com.geovis.emergency.spd.entity.pc.pojo.enums.TaskStatusEnum;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 应急预案指挥模块-演练工作方案详情 服务实现类
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/7/6
 */
@Service
@AllArgsConstructor
public class PcDrillPlanDetailServiceImpl extends ServiceImpl<PcDrillPlanDetailMapper, PcDrillPlanDetail> implements IPcDrillPlanDetailService {

    private final IPcDrillDisasterEventService pcDrillDisasterEventService;
    private final PcDrillPlanTaskMapper pcDrillPlanTaskMapper;

    @Override
    public PageResult<DrillPlanTaskMobileVO> mobileTaskPage(String pcDrillId, String userId, PageParam pageParam) {
        LambdaQueryWrapper<PcDrillPlanDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcDrillPlanDetail::getPcDrillId,pcDrillId);
        wrapper.eq(PcDrillPlanDetail::getStatus, CheckStatusEnum.PASS.getCode());
        wrapper.eq(PcDrillPlanDetail::getPrincipalId, AuthUtil.getCurrentUserId());
        wrapper.orderByDesc(PcDrillPlanDetail::getCreateTime);
        IPage<PcDrillPlanDetail> pageBuilder = pageParam.buildPage();
        IPage<PcDrillPlanDetail> page = page(pageBuilder, wrapper);
        Date today = DateUtil.date(),yesterday = DateUtil.yesterday();
        List<DrillPlanTaskMobileVO> collect = page.getRecords().stream().map(e ->{
            DrillPlanTaskMobileVO vo = covert(e, today, yesterday);
            String status = pcDrillPlanTaskMapper.selectStatusByPlanId(e.getId(), e.getPcDrillId());
            if(status==null){
                vo.setStatus(TaskStatusEnum.WAIT_COMMIT.getCode());
            }else{
                vo.setStatus(status);
            }
            return vo;
        }).collect(Collectors.toList());
        IPage<DrillPlanTaskMobileVO> resultPage = new Page();
        BeanUtil.copyProperties(page,resultPage);
        resultPage.setRecords(collect);
        return new PageResult<>(resultPage);
    }

    /**
     * 将实体类转换成对应VO类
     * @param pcDrillPlanDetail
     * @return
     */
    private DrillPlanTaskMobileVO covert(PcDrillPlanDetail pcDrillPlanDetail, Date today,Date yesterday){
        DrillPlanTaskMobileVO vo = new DrillPlanTaskMobileVO();
        vo.setTaskId(pcDrillPlanDetail.getId());
        vo.setContent(pcDrillPlanDetail.getContent());
        vo.setStatus(pcDrillPlanDetail.getStatus());
        Date createTime = Date.from(pcDrillPlanDetail.getCreateTime().atZone(ZoneId.systemDefault()).toInstant());
        vo.setShowTime(DateUtil.format(createTime,"HH:mm"));
        if(DateUtil.isSameDay(today,createTime)){
            vo.setTimePeriod("今天");
        }else if(DateUtil.isSameDay(yesterday,createTime)){
            vo.setTimePeriod("昨天");
        }else{
            vo.setTimePeriod(DateUtil.formatDate(createTime));
        }
        PcDrillDisasterEvent disasterEvent = pcDrillDisasterEventService.getById(pcDrillPlanDetail.getPcDrillDisasterEventId());
        if(disasterEvent!=null){
            vo.setTitle(disasterEvent.getTitle());
            String dictValue = DictCache.getDictValue(PcDisasterConstant.DISASTER_TYPE, disasterEvent.getType());
            vo.setDisasterType(dictValue);
        }
        return vo;
    }
}
